package com.zone.sdk_api;

import android.content.Context;
import android.util.Log;

import com.saicmotor.pay.entity.OrderEntity;
import com.saicmotor.pay.entity.PayEntity;
import com.saicmotor.pay.entity.PayResultEntity;
import com.saicmotor.pay.request.OrderSubmitRequest;
import com.saicmotor.pay.request.PayOrderRequest;
import com.saicmotor.pay.request.QueryPayResultRequest;
import com.zone.sdk.common.http.base.BaseRequest;
import com.zone.sdk.common.http.base.PageResult;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.sdk.module.account.entity.CaptchaCertExchangeEntity;
import com.zone.sdk.module.account.entity.CaptchaGetEntity;
import com.zone.sdk.module.account.entity.CheckCaptchaEntity;
import com.zone.sdk.module.account.entity.LoginEntity;
import com.zone.sdk.module.account.entity.LoginStatusEntity;
import com.zone.sdk.module.account.entity.QrLoginEntity;
import com.zone.sdk.module.account.entity.QrLoginPollEntity;
import com.zone.sdk.module.account.entity.UploadEntity;
import com.zone.sdk.module.account.entity.UserInfoEntity;
import com.zone.sdk.module.account.request.CaptchaCertExchangeRequest;
import com.zone.sdk.module.account.request.CaptchaGetRequest;
import com.zone.sdk.module.account.request.CheckCaptchaRequest;
import com.zone.sdk.module.account.request.LoginRequest;
import com.zone.sdk.module.account.request.LogoutRequest;
import com.zone.sdk.module.account.request.QrLoginPollRequest;
import com.zone.sdk.module.account.request.QrLoginRequest;
import com.zone.sdk.module.account.request.RefreshTokenRequest;
import com.zone.sdk.module.account.request.UploadRequest;
import com.zone.sdk.module.account.request.UserInfoRequest;
import com.zone.sdk.module.account.response.CaptchaGetResponse;
import com.zone.sdk.module.appstore.base.AppStoreResult;
import com.zone.sdk.module.appstore.entity.AppDetailEntity;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.sdk.module.appstore.entity.AppListItemEntity;
import com.zone.sdk.module.appstore.request.AppsDetailRequest;
import com.zone.sdk.module.comment.base.CommentPage;
import com.zone.sdk.module.comment.entity.AppCommentScoreEntity;
import com.zone.sdk.module.comment.entity.CommentListEntity;
import com.zone.sdk.module.comment.entity.RecommendEntity;
import com.zone.sdk.module.comment.entity.RecommendListEntity;
import com.zone.sdk.module.comment.entity.SearchResultEntity;
import com.zone.sdk.module.comment.request.AppSearchRequest;
import com.zone.sdk.module.comment.request.CommentDetailRequest;
import com.zone.sdk.module.comment.request.CommentListRequest;
import com.zone.sdk.module.comment.request.CommentScoreRequest;
import com.zone.sdk.module.comment.request.RecommendListRequest;
import com.zone.vehicle.sdk.module.superapp.cloud.SuperAppManager;
import com.zone.vehicle.sdk.module.superapp.entity.AppUpdateEntity;
import com.zone.vehicle.sdk.module.superapp.request.AppPushRequest;
import com.zone.vehicle.sdk.module.superapp.request.AppsDownloadRequest;
import com.zone.vehicle.sdk.module.superapp.request.AppsUpdateListRequest;
import com.zone.vehicle.sdk.module.superapp.request.SyncStatusRequest;
import com.zone.vehicle.sdk.module.superapp.response.AppPushResponse;
import com.zone.vehicle.sdk.module.superapp.vehicle.utils.NetUtil;

import java.util.List;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.http.Body;
import retrofit2.http.Part;

import static com.zone.sdk_api.common.Content.USER_ID;
import static com.zone.sdk_api.common.Content.VIN;

public class ApiManager {

    private static ApiManager apiManager;

    private SuperAppManager superAppManager;
//    private SuperAppVehicleServiceManager vehicleServiceManager;
    private Context context;

    public static ApiManager getInstance() {
        if (apiManager == null){
            throw new NullPointerException("api manager need init first!");
        }
        return apiManager;
    }

    public void setHeader(String key, String value) {
         superAppManager.setHeader(key, value);
    }

    public synchronized static ApiManager init(String url, Context context) {
        if (apiManager == null){
            apiManager = new ApiManager(url, context);
        }
        return apiManager;
    }

    private ApiManager(String url, Context context) {
        this.context = context;
        superAppManager = SuperAppManager.getInstance(url);
//        vehicleServiceManager = SuperAppVehicleServiceManager.getInstance();
    }


    /**********************************SuperApp*********************************************/
    /**
     * 应用下载
     */
    public void appsDownload(AppsDownloadRequest request, ResultCallBack<ResponseBody> callback){
//        if (NetUtil.inspectNet(context)){
            superAppManager.appsDownload(request, callback);
//        }
    }
    /**
     * 应用状态同步
     */
    public void syncStatus(SyncStatusRequest request, ResultCallBack<Result> callback){
        superAppManager.syncStatus(request, callback);
    }

    public void appPush(AppPushRequest request, ResultCallBack<Result<AppPushResponse>> callback){
        superAppManager.appPush(request, callback);
    }

    public void updateList(AppsUpdateListRequest request, ResultCallBack<Result<List<AppUpdateEntity>>> callback){
        superAppManager.updateList(request, callback);
    }

    /*******************************************************************************/
    /**********************************AppStore*********************************************/
    public void homeAppList(BaseRequest request, ResultCallBack<Result<AppStoreResult<AppListItemEntity>>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.homeAppList(request, callback);
//        }
    }
    /**
     * 应用详情
     */
    public void appsDetail(AppsDetailRequest request, ResultCallBack<Result<AppDetailEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
        Log.d("app detail ", "app detail " + request);
            superAppManager.appsDetail(request, callback);
//        }
    }

    public void appList(AppsDetailRequest request, ResultCallBack<Result<List<AppEntity>>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.appList(request, callback);
//        }
    }
    public void submitOrder(OrderSubmitRequest request, ResultCallBack<Result<OrderEntity>> callback){
        superAppManager.submitOrder(request, callback);
    }

    public void payOrder(PayOrderRequest request, ResultCallBack<Result<PayEntity>> callback){
        superAppManager.payOrder(request, callback);
    }

    public void queryPayResult(QueryPayResultRequest request, ResultCallBack<Result<PayResultEntity>> callback){
        superAppManager.queryPayResult(request, callback);
    }

    /**
     * 根据appids 批量获取app详情
     */
    public void getRecommendListApp(RecommendListRequest request, ResultCallBack<Result<PageResult<RecommendEntity>>> callback){
//        superAppManager.appsDetail(request, callback);
    }
    /*******************************************************************************/
    /**********************************搜索、评论模块*********************************************/


    /*******************************************************************************/

    public void indexSearch(AppSearchRequest request, ResultCallBack<Result<CommentPage<SearchResultEntity>>> callback){
        if (NetUtil.inspectNet(context)) {
            superAppManager.indexSearch(request, callback);
        }
    }

    /**
     * 显示app总体评分和所有用户评价列表
     *
     */
    public void commentScore(CommentScoreRequest request, ResultCallBack<Result<AppCommentScoreEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.commentScore(request, callback);
//        }
    }

    /**
     * 显示app总体评分和所有用户评价列表
     *
     *
     */
    public void commentCommentList(CommentListRequest request, ResultCallBack<Result<CommentListEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.commentCommentList(request, callback);
//        }
    }

    /**
     * 显示app总体评分和所有用户评价列表
     *
     *
     */
    public void commentCommentDetail(CommentDetailRequest request, ResultCallBack<Result> callback){
        superAppManager.commentCommentDetail(request, callback);
    }

    /**
     * 分页显示安利墙列表
     *
     *
     */
    public void commentInitAmwayWallList(RecommendListRequest request, ResultCallBack<Result<RecommendListEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.commentInitAmwayWallList(request, callback);
//        }
    }

    /**
     * 用户查看自己评论过的信息列表
     *
     *
     */
    public void commentUserCommentList(CommentListRequest request, ResultCallBack<Result<CommentListEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.commentUserCommentList(request, callback);
//        }
    }

    /**
     * 用户删除自己的评论
     *
     *
     */
    public void commentDelUserComment(BaseRequest request, ResultCallBack<Result> callback){
        superAppManager.commentDelUserComment(request, callback);
    }

    /**
     * 查询应用下评论数，不包含回复和回复回复
     *
     *
     */
    public void commentSearchCommentCount(BaseRequest request, ResultCallBack<Result> callback){
        superAppManager.commentSearchCommentCount(request, callback);
    }

    /**
     * 用户提交意见反馈
     */
    public void feedbackFeedbackCommit(BaseRequest request, ResultCallBack<Result> callback){
        superAppManager.feedbackFeedbackCommit(request, callback);
    }

    /**********************************账号*************************************/

    public void captchaGet(CaptchaGetRequest request, ResultCallBack<CaptchaGetResponse<CaptchaGetEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.captchaGet(request, callback);
//        }
    }

    public void checkCaptcha(CheckCaptchaRequest request, ResultCallBack<CaptchaGetResponse<CheckCaptchaEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.checkCaptcha(request, callback);
//        }
    }
    /**
     * 车机端同步下载队列应用并由云端推送到手机端
     * /user/captchacert/exchange/v1
     */
    public void captchacertExchange(CaptchaCertExchangeRequest request, ResultCallBack<Result<CaptchaCertExchangeEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.captchacertExchange(request, callback);
//        }
    }

    /**
     * 手机号+验证码登录/注册/忘记密码流程中，调用此接口获取短信验证码
     */
    public void loginPhoneCode(LoginRequest request, ResultCallBack<Result<LoginEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.loginPhoneCode(request, callback);
//        }
    }
    /**
     * 手机号+验证码登录/注册/忘记密码流程中，调用此接口获取短信验证码
     * 与获取验证码接口相同，请求参数做区分 loginPhoneCode
     */
    public void loginPhoneCodeCheck(LoginRequest request, ResultCallBack<Result<LoginEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.loginPhoneCodeCheck(request, callback);
//        }
    }

    /**
     * 手机号+密码登录流程中，调用此接口提交登录请求，并获取登录信息
     */
    public void loginPhonePwd(LoginRequest request, ResultCallBack<Result<LoginEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.loginPhonePwd(request, callback);
//        }
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口设置密码。
     */
    public void userPwdModify(LoginRequest request, ResultCallBack<Result> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.userPwdModify(request, callback);
//        }
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口获取个人基本信息
     */
    public void queryUserInfo(ResultCallBack<Result<UserInfoEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.queryUserInfo(callback);
//        }
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口修改个人基本信息。
     */
    public void updateUserInfo(UserInfoRequest request, ResultCallBack<Result> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.updateUserInfo(request, callback);
//        }
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口退出当前账号
     */
    public void logout(LogoutRequest request, ResultCallBack<Result> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.logout(request, callback);
//        }
    }

    /**
     * 用户Access Token 过期状态下，调用此接口以获取新的Token。
     */
    public void refreshToken(RefreshTokenRequest request, ResultCallBack<Result<LoginEntity>> callback){
        superAppManager.refreshToken(request, callback);
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口上传文件（移动端用于上传自定义头像）。
     */
    public void userUpload(@Part("files") RequestBody body, @Body UploadRequest request, ResultCallBack<Result<UploadEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.userUpload(body, request, callback);
//        }
    }

    /**
     * 用户持有有效Access Token 状态下，调用此接口查询当前账户在所有设备上的登录状态
     */
    public void loginStatus(ResultCallBack<Result<LoginStatusEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.loginStatus(callback);
//        }
    }

    /**
     * 车机端调用此接口生成登录二维码，返回的是QR Code URL
     */
    public void qrLoginCode(QrLoginRequest request, ResultCallBack<Result<QrLoginEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.qrLoginCode(request, callback);
//        }
    }

    /**
     * 车机端通过此接口轮询登录状态。
     */
    public void qrLoginPolling(QrLoginPollRequest request, ResultCallBack<Result<QrLoginPollEntity>> callback){
//        if (NetUtil.inspectNet(context)) {
            superAppManager.qrLoginPolling(request, callback);
//        }
    }

}
